package cn.kpi.service.vtiondpline.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.vtiondpline.VtDpUserDao;
import cn.kpi.entity.vtiondpline.VtDpUserEntity;
import cn.kpi.entity.vtiondpline.VtDpUserListEntity;
import cn.kpi.entity.vtiondpline.VtDpUserLogEntity;
import cn.kpi.service.vtiondpline.IVtDpUserService;

@Service
public class VtDpUserServiceImpl implements IVtDpUserService {
	@Autowired
	private VtDpUserDao vtDpUserDao;

	@Transactional
	public int InseretVtdpUser(VtDpUserEntity entity, VtDpUserLogEntity entitylog) throws Exception {
		try {
			int result = 0;
			result += vtDpUserDao.InseretVtdpUser(entity);
			Long uservtdpid = entity.getUservtdpid();
			entitylog.setUservtdpid(uservtdpid);
			result += vtDpUserDao.InsertVtdpUserLog(entitylog);
			if (result != 2) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}

			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	public List<VtDpUserListEntity> GetUserVtDpList(String where) throws Exception {
		try {
			return vtDpUserDao.GetUserVtDpList(where);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public int GetUserVtDpListCount(String where) throws Exception {
		try {
			return vtDpUserDao.GetUserVtDpListCount(where);
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	@Transactional
	public int UpdateVtdpUser(Long userid, VtDpUserLogEntity entitylog) throws Exception {
		try {
			int result = 0;
			Long uservtdpid = entitylog.getUservtdpid();
			String uservtdpdes = entitylog.getUservtdplogdes();
			int status = entitylog.getStatus();
			result += vtDpUserDao.UpdateVtDpUser(uservtdpid, userid, uservtdpdes, status, entitylog.getByuserid());

			result += vtDpUserDao.InsertVtdpUserLog(entitylog);
			if (result != 2) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return -1;
			}

			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	public List<VtDpUserListEntity> GetVtDpListByUserid(Long userid) throws Exception {
		try {
			return vtDpUserDao.GetVtDpListByUserid(userid);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public VtDpUserListEntity GetUserVtDpByUservtdpid(Long uservtdpid) throws Exception {
		try {
			return vtDpUserDao.GetUserVtDpByUservtdpid(uservtdpid);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public int UpdateVtDpUserEntity(VtDpUserEntity entity) throws Exception {
		try {
			return vtDpUserDao.UpdateVtDpUserEntity(entity);
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}

	public int DeleteVtDpUser(Long uservtdpid) throws Exception {
		try {
			return vtDpUserDao.DeleteVtDpUser(uservtdpid);
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}
	public List<VtDpUserListEntity> GetUserVtDpBcjkhList(String where)throws Exception{
		try {
			return vtDpUserDao.GetUserVtDpBcjkhList(where);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	public int GetUserVtDpBcjkhListCount(String where)throws Exception{
		try {
			return vtDpUserDao.GetUserVtDpBcjkhListCount(where);
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
	}
	
	public List<VtDpUserListEntity> GetVtDpListByUseridAndWhere(Long userid,String where)throws Exception{
		try {
			return vtDpUserDao.GetVtDpListByUseridAndWhere(userid,where);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
